package guilib;

/***
 * @author Ragnvald Fuglestein
 */

import javax.swing.table.AbstractTableModel;

import callback.Callback;
import callback.TrackListChangedInterface;

import baseStructure.Album;
import baseStructure.Track;

import library.Library;
import library.PlayList;

public class PlaylistTableModel extends AbstractTableModel implements TrackListChangedInterface {
	private static final long serialVersionUID = 6129064172758625006L;

	private PlayList list = null;
	
	private String[] columnNames = {
			"Artist",
			"Album",
			"Track",
	};
	
    public PlaylistTableModel(String name) {
    	if (Library.getInstance().playlists.containsKey(name)) {
    		this.list = Library.getInstance().playlists.get(name);
    	} else {
	    	this.list = new PlayList(name);
	    	Library.getInstance().playlists.put(name, list);
    	}
    	
    	Callback.registerTrackListChanged(name, this);
    }

    public void remove(int row) {
    	list.remove(row);
    }
    
    /*public void remove(int[] rows) {
    	list.remove(rows[0], rows[rows.length - 1]);
    }*/
    
    @Override public void tracklistChanged() {
    	fireTableDataChanged();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return list.size();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    	Track track = list.get(row);
		
    	if (columnNames[col].equals("Artist")) { return track.getArtist().getName(); }
    	else if (columnNames[col].equals("Album")) { 
    		Album a = track.getAlbum();
    		if (a == null) return "";
    		else return a.getName();
    	}
    	else if (columnNames[col].equals("Track")) { return track.getName(); }
    	
    	else {
    		return "";
    	}
    }
    
	public Class<? extends Object> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    public boolean isCellEditable(int row, int col) {
    	/*if (columnNames[col].equals("Artist")) { return false; }
    	else if (columnNames[col].equals("Album")) { return false; }
    	else if (columnNames[col].equals("Year")) { return false; }
    	else if (columnNames[col].equals("Name")) { return false; }
    	else if (columnNames[col].equals("Track")) { return false; }*/
    	
    	return false;
    }

    public void setValueAt(Object value, int row, int col) {
        //TODO
    	/*data[row][col] = value;
        fireTableCellUpdated(row, col);*/
    }

}
